package com.mindainfo.groundwave.server.base.aop;

import com.mindainfo.groundwave.common.bean.CommonResult;
import com.mindainfo.groundwave.common.utils.ValidateUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2017/11/15.
 * 前置通知拦截
 * 对添加了@NeedValidate注解的方法进行参数校验
 */
@Aspect
@Component
public class ValidateAspect {

    @Pointcut("@annotation(com.mindainfo.groundwave.server.base.annotation.NeedValidate)")
    public void targetMethod(){

    }

    @Around("targetMethod()")
    public Object aroundMethod(ProceedingJoinPoint point){

        Object param = point.getArgs()[0];
        CommonResult<Object> validate = ValidateUtil.validate(param);
        if(validate.getCode()!=0){
            return validate;
        }
        Object proceed = null;
        try {
            proceed = point.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }
}
